 
package c.x.jy.common.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpStatus;

/**
 * 返回数据
 *
  
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	public R() {
		put("code", 0);
		put("msg", "success");
	}
	
	public static R error() {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
	}
	
	public static R error(String msg) {
		return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
	}
	
	public static R error(int code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("msg", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("msg", msg);
		
		return r;
	}
	
	public static R ok(Map<String, Object> map) {
		R r = new R();
		r.putAll(map);
		return r;
	}
	
	public static R ok() {
		return new R();
	}

	public  R put(String key, Object value) {
		if(value instanceof PageUtils) {
			super.put(key, ((PageUtils)value).getList());
			super.put("currPage", ((PageUtils)value).getCurrPage());
			super.put("totalCount", ((PageUtils)value).getTotalCount());
			super.put("count", ((PageUtils)value).getTotalCount());	
	 
			super.put("limit", ((PageUtils)value).getPageSize());
			super.put("curr",((PageUtils)value).getCurrPage());	
			
		}else
		super.put(key, value);
		return this;
	}
	public  static R put(  Object value) {
		//new R().put("data", value);
		 R r=R.ok();
		 r.put("code", 0);
		if(value instanceof PageUtils) {
		 
			r.put("data", ((PageUtils)value).getList());
			r.put("currPage", ((PageUtils)value).getCurrPage());
			r.put("totalCount", ((PageUtils)value).getTotalCount());
			r.put("pageSize", ((PageUtils)value).getPageSize());
			r.put("totalPage", ((PageUtils)value).getTotalPage());
			r.put("size", ((PageUtils)value).getList().size());
			r.put("count", ((PageUtils)value).getTotalCount());
			r.put("limit", ((PageUtils)value).getPageSize());
			r.put("curr",((PageUtils)value).getCurrPage());	
		 
		}else if(value instanceof List) {
			r.put("data", value);
			r.put("size", ((List)value).size());
		}else {
			r.put("data", value);
		}
		
		return r;
	}
}
